package top.pmwly.CSRF.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName LoginServiceImpl.java
 * @Description 登录服务
 * @createTime 2021年08月17日 15:58:00
 */
@Service
public class LoginServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) {
        //数据库查用户名  此时没用到数据库模拟一下
        if (!"admin".equals(username)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //校验密码
        String encode = passwordEncoder.encode("123");
        //返回对象
        return new User(username, encode, AuthorityUtils.
                commaSeparatedStringToAuthorityList("admin,normal,ROLE_admin,/insert,/delete"));
    }
}
